package com.jeecg.p3.system.poster.task;

import com.jeecg.p3.base.util.DateUtil;
import com.jeecg.p3.system.order.entity.ProductDetails;
import com.jeecg.p3.system.order.service.ProductDetailsService;
import com.jeecg.p3.system.poster.entity.GradesNum;
import com.jeecg.p3.system.poster.entity.ScanCodeNum;
import com.jeecg.p3.system.poster.service.GradesNumService;
import com.jeecg.p3.system.poster.service.PosterManagerService;
import com.jeecg.p3.system.poster.service.ScanCodeNumService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 海报，考试等相关处理类
 * @author zhangxiao
 * @version 2020-12-16
 */
@Component
@EnableScheduling
public class PosterTask {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ScanCodeNumService scanCodeNumService;
    @Autowired
    private PosterManagerService posterManagerService;
    @Autowired
    private GradesNumService gradesNumService;
    @Autowired
    private ProductDetailsService productDetailsService;

    /**
     * 更新前一天的扫码数量
     */
    @Scheduled(cron = "0 1 0 * * *")
    public void updateScanCodeNum() {
        logger.info("----------更新前一天的扫码开始数量--------");
        try {
            Integer num = posterManagerService.findBeforeDayScanNum();
            ScanCodeNum scanCodeNum = new ScanCodeNum();
            scanCodeNum.setNum(num != null ? num.toString() : "0");
            Date date = new Date();
            scanCodeNum.setCreateDate(DateUtil.getBeforeDate(DateUtil.dateToString(date)));
            scanCodeNum.setCreateTime(DateUtil.dateToString(date, "HH:mm:ss"));
            scanCodeNumService.doAdd(scanCodeNum);
        } catch (Exception e) {
            logger.error("更新前一天的扫码数量异常！");
            e.printStackTrace();
        }
        logger.info("----------更新前一天的扫码数量结束--------");
    }

    /**
     * 更新前一天的交易数量
     */
    @Scheduled(cron = "30 1 0 * * *")
    public void updateGradesNum() {
        logger.info("----------更新前一天的交易数量开始--------");
        try {
            Date date = new Date();
            String beforeDate = DateUtil.getBeforeDate(DateUtil.dateToString(date));
            ProductDetails param = new ProductDetails();
            param.setCreateDate(beforeDate);
            param.setUserStatus(1);
            Integer num = productDetailsService.count(param);//前一日交易数量
            GradesNum gradesNum = new GradesNum();
            gradesNum.setNum(num != null ? num.toString() : "0");
            gradesNum.setCreateDate(beforeDate);
            gradesNum.setCreateTime(DateUtil.dateToString(date, "HH:mm:ss"));
            gradesNumService.doAdd(gradesNum);
        } catch (Exception e) {
            logger.error("更新前一天的交易数量异常！");
            e.printStackTrace();
        }
        logger.info("----------更新前一天的交易数量结束--------");
    }

}
